package com.zenithmind.classroom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.classroom.pojo.domain.Course;
import com.zenithmind.classroom.pojo.vo.CourseStatisticsVO;
import com.zenithmind.classroom.pojo.vo.CourseVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 课程Mapper接口
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface CourseMapper extends BaseMapper<Course> {

    /**
     * 分页查询课程（包含关联信息）
     * 
     * @param page 分页参数
     * @param keyword 关键词
     * @param categoryId 分类ID
     * @param instructorId 讲师ID
     * @param status 状态
     * @param difficulty 难度
     * @param type 类型
     * @param isFree 是否免费
     * @param isRecommend 是否推荐
     * @param isHot 是否热门
     * @param isNew 是否新课程
     * @param minPrice 最低价格
     * @param maxPrice 最高价格
     * @return 分页结果
     */
    IPage<CourseVO> selectCoursePageWithDetails(
            Page<CourseVO> page,
            @Param("keyword") String keyword,
            @Param("categoryId") String categoryId,
            @Param("instructorId") String instructorId,
            @Param("status") Integer status,
            @Param("difficulty") Integer difficulty,
            @Param("type") Integer type,
            @Param("isFree") Integer isFree,
            @Param("isRecommend") Integer isRecommend,
            @Param("isHot") Integer isHot,
            @Param("isNew") Integer isNew,
            @Param("minPrice") Double minPrice,
            @Param("maxPrice") Double maxPrice
    );

    /**
     * 获取课程详情（包含关联信息）
     * 
     * @param courseId 课程ID
     * @param userId 用户ID
     * @return 课程详情
     */
    CourseVO selectCourseDetailWithUser(@Param("courseId") String courseId, @Param("userId") String userId);

    /**
     * 获取推荐课程
     * 
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 课程列表
     */
    List<CourseVO> selectRecommendCourses(@Param("userId") String userId, @Param("limit") Integer limit);

    /**
     * 获取热门课程
     * 
     * @param limit 限制数量
     * @return 课程列表
     */
    @Select("SELECT c.*, " +
            "u.real_name as instructor_name, u.avatar as instructor_avatar, " +
            "cat.name as category_name " +
            "FROM classroom_course c " +
            "LEFT JOIN user u ON c.instructor_id = u.id " +
            "LEFT JOIN course_category cat ON c.category_id = cat.id " +
            "WHERE c.status = 1 AND c.deleted = 0 AND c.is_hot = 1 " +
            "ORDER BY c.student_count DESC, c.rating DESC " +
            "LIMIT #{limit}")
    List<CourseVO> selectHotCourses(@Param("limit") Integer limit);

    /**
     * 获取最新课程
     * 
     * @param limit 限制数量
     * @return 课程列表
     */
    @Select("SELECT c.*, " +
            "u.real_name as instructor_name, u.avatar as instructor_avatar, " +
            "cat.name as category_name " +
            "FROM classroom_course c " +
            "LEFT JOIN user u ON c.instructor_id = u.id " +
            "LEFT JOIN course_category cat ON c.category_id = cat.id " +
            "WHERE c.status = 1 AND c.deleted = 0 " +
            "ORDER BY c.publish_time DESC " +
            "LIMIT #{limit}")
    List<CourseVO> selectLatestCourses(@Param("limit") Integer limit);

    /**
     * 搜索课程
     * 
     * @param keyword 关键词
     * @param limit 限制数量
     * @return 课程列表
     */
    List<CourseVO> searchCourses(@Param("keyword") String keyword, @Param("limit") Integer limit);

    /**
     * 获取用户课程
     * 
     * @param userId 用户ID
     * @param status 状态
     * @param limit 限制数量
     * @return 课程列表
     */
    List<CourseVO> selectUserCourses(@Param("userId") String userId, @Param("status") Integer status, @Param("limit") Integer limit);

    /**
     * 获取讲师课程
     * 
     * @param instructorId 讲师ID
     * @param limit 限制数量
     * @return 课程列表
     */
    @Select("SELECT c.*, " +
            "u.real_name as instructor_name, u.avatar as instructor_avatar, " +
            "cat.name as category_name " +
            "FROM classroom_course c " +
            "LEFT JOIN user u ON c.instructor_id = u.id " +
            "LEFT JOIN course_category cat ON c.category_id = cat.id " +
            "WHERE c.instructor_id = #{instructorId} AND c.deleted = 0 " +
            "ORDER BY c.create_time DESC " +
            "LIMIT #{limit}")
    List<CourseVO> selectInstructorCourses(@Param("instructorId") String instructorId, @Param("limit") Integer limit);

    /**
     * 获取分类课程
     * 
     * @param categoryId 分类ID
     * @param limit 限制数量
     * @return 课程列表
     */
    @Select("SELECT c.*, " +
            "u.real_name as instructor_name, u.avatar as instructor_avatar, " +
            "cat.name as category_name " +
            "FROM classroom_course c " +
            "LEFT JOIN user u ON c.instructor_id = u.id " +
            "LEFT JOIN course_category cat ON c.category_id = cat.id " +
            "WHERE c.category_id = #{categoryId} AND c.status = 1 AND c.deleted = 0 " +
            "ORDER BY c.student_count DESC " +
            "LIMIT #{limit}")
    List<CourseVO> selectCategoryCourses(@Param("categoryId") String categoryId, @Param("limit") Integer limit);

    /**
     * 获取相关课程
     * 
     * @param courseId 课程ID
     * @param limit 限制数量
     * @return 课程列表
     */
    List<CourseVO> selectRelatedCourses(@Param("courseId") String courseId, @Param("limit") Integer limit);

    /**
     * 获取收藏课程
     * 
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 课程列表
     */
    List<CourseVO> selectFavoriteCourses(@Param("userId") String userId, @Param("limit") Integer limit);

    /**
     * 更新课程统计
     * 
     * @param courseId 课程ID
     * @param studentCount 学生数增量
     * @param completedCount 完成数增量
     * @return 影响行数
     */
    @Update("UPDATE classroom_course SET " +
            "student_count = student_count + #{studentCount}, " +
            "completed_count = completed_count + #{completedCount}, " +
            "update_time = NOW() " +
            "WHERE id = #{courseId}")
    int updateCourseStatistics(@Param("courseId") String courseId, 
                              @Param("studentCount") Long studentCount, 
                              @Param("completedCount") Long completedCount);

    /**
     * 更新课程评分
     * 
     * @param courseId 课程ID
     * @param rating 新评分
     * @param reviewCount 评价数量
     * @return 影响行数
     */
    @Update("UPDATE classroom_course SET " +
            "rating = #{rating}, " +
            "review_count = #{reviewCount}, " +
            "update_time = NOW() " +
            "WHERE id = #{courseId}")
    int updateCourseRating(@Param("courseId") String courseId, 
                          @Param("rating") Double rating, 
                          @Param("reviewCount") Long reviewCount);

    /**
     * 获取课程统计信息
     * 
     * @param courseId 课程ID
     * @return 统计信息
     */
    CourseStatisticsVO selectCourseStatistics(@Param("courseId") String courseId);

    /**
     * 批量更新课程章节数量
     * 
     * @param courseId 课程ID
     * @return 影响行数
     */
    @Update("UPDATE classroom_course SET " +
            "chapter_count = (SELECT COUNT(*) FROM classroom_course_chapter WHERE course_id = #{courseId} AND deleted = 0), " +
            "lesson_count = (SELECT COUNT(*) FROM classroom_course_lesson WHERE course_id = #{courseId} AND deleted = 0), " +
            "update_time = NOW() " +
            "WHERE id = #{courseId}")
    int updateCourseContentCount(@Param("courseId") String courseId);

    /**
     * 检查用户是否收藏了课程
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 是否收藏
     */
    @Select("SELECT COUNT(*) > 0 FROM course_favorite WHERE user_id = #{userId} AND course_id = #{courseId} AND deleted = 0")
    Boolean checkUserFavoriteCourse(@Param("userId") String userId, @Param("courseId") String courseId);

    /**
     * 获取用户对课程的评分
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 评分
     */
    @Select("SELECT rating FROM course_review WHERE user_id = #{userId} AND course_id = #{courseId} AND deleted = 0 LIMIT 1")
    Integer getUserCourseRating(@Param("userId") String userId, @Param("courseId") String courseId);

    /**
     * 获取课程学习进度
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 进度百分比
     */
    @Select("SELECT COALESCE(progress_percentage, 0) FROM learning_progress " +
            "WHERE user_id = #{userId} AND course_id = #{courseId} AND deleted = 0 LIMIT 1")
    Integer getUserCourseProgress(@Param("userId") String userId, @Param("courseId") String courseId);

    /**
     * 检查用户是否已报名课程
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 是否已报名
     */
    @Select("SELECT COUNT(*) > 0 FROM course_enrollment WHERE user_id = #{userId} AND course_id = #{courseId} AND deleted = 0")
    Boolean checkUserEnrolledCourse(@Param("userId") String userId, @Param("courseId") String courseId);
}
